﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {

        private String localUrl;
        private String port;
        private String serverUrl;

        public Login()
        {
            String[] args = Environment.GetCommandLineArgs();
            if (args.Length != 1)
            {
                if (args[1] != "null")
                {
                    serverUrl = args[1];
                }
                else
                {
                    serverUrl = "localhost:8008";
                }
                if (args.Length != 2)
                {
                    port = args[2];
                }
                else
                {
                    port = "8080";
                }
            }
            else
            {
                serverUrl = "localhost:8008";
                port = "8080";
            }
            localUrl = HTTP.LocalIPAddress();
            new HTTP(serverUrl);
            InitializeComponent();
            loginUsernameTxtbox.Focusable = true;
            loginUsernameTxtbox.Focus();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            loginUser();
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            String username = signupuserNameTBox.Text;
            String salt = Authentication.getRandomSaltKey();
            String password = Authentication.sha256_hash(salt, signupPasswordSignupBox.Password);
            String playername = signupNameTBox.Text;
            String email = signupEmailTBox.Text;
            String parameters = "username="+username + "&password=" + password;
            if (playername != "" || email != "")
            {
                if (email == "") email = "null";
                if (playername == "") playername = "null";
                parameters += "&playername=" + playername + "&email=" + email;
            }
            String success = HTTP.GETfromServer("register-user", parameters);
            if (success == "success")
            {
                success = HTTP.GETfromServer("register-user-salt", "username=" + username + "&salt=" + salt);
                if (success == "success")
                {
                    MessageBox.Show("Successfully created your account");
                    loginUser();
                }
            }
            if(success == "failure")
            {
                MessageBox.Show("Wasn't able to create user.");
            }
            
        }

        private void loginUser()
        {
            String username = loginUsernameTxtbox.Text;
            String salt = HTTP.GETfromServer("get-salt", username);
            String password = Authentication.sha256_hash(salt, loginPasswordTxtbox.Password);
            
            String success = HTTP.GETfromServer("check-password", "type=player&subtype=add&username=" + username + "&password=" + password + "&ipaddress=" + localUrl + ":"+port );

            if (success == "success")
            {
                (new Lobby(loginUsernameTxtbox.Text != "" ? loginUsernameTxtbox.Text : signupuserNameTBox.Text, localUrl+":"+port, serverUrl)).Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("Error occured while trying to log you in.");
            }
        }

        private void loginPassword_OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                loginUser();
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            signupEmailTBox.Text = "";
            signupNameTBox.Text = "";
            signupPasswordSignupBox.Password = "";
            signupuserNameTBox.Text = "";
            loginTab.SelectedIndex = 0 ;
        }
    }
}

//if(request.getUri().toString().equals("/get-salt""/check-password""/register-user"/register-user-salt"
